<?php
namespace app\models;

use Yii;
use app\modules\developer\models\AppInfo;

class Opened extends \yii\db\ActiveRecord
{
    public $buyTime;//购买时长
    
    public static function tableName()
    {
        return '{{%opened}}';
    }

    public function rules()
    {
        return [
            ['app_info_id', 'required', 'message' => 'app_info_id不能为空', 'on' => 'add'],
            ['app_info_id', 'integer', 'message' => 'app_info_id必须为数字，或数字字符串', 
                            'min'=>1, 'tooSmall' => 'app_info_id最小存储1', 
                            'max'=>65535, 'tooBig' => 'app_info_id最大存储65535'],
            ['app_name', 'required', 'message' => 'app_name不能为空', 'on' => 'add'],
            ['app_name', 'string', 'message' => 'app_name必须是字符串',
                         'min' => 2, 'tooShort' => 'app_name最短2位',
                         'max' => 64, 'tooLong' => 'app_name最长64位', 'on' => 'add'],
            ['buyTime', 'required', 'message' => 'buyTime不能为空', 'on' => 'add'],
            ['buyTime', 'integer', 'message' => 'buyTime必须为数字，或数字字符串'],
            ['buyTime', 'in', 'range' => array(0, 1, 2, 3, 5, 10, 99),
                        'message' => 'buyTime值必须在0,1,2,3,4,5,10,20中选择'],
            ['describe', 'default', 'value' => ''],
            ['describe', 'string', 'message' => 'describe必须是字符串'],
            ['comid', 'required', 'message' => 'comid不能为空', 'on' => 'add'],
            ['comid', 'string', 'message' => 'comid必须是字符串',
                      'min' => 8, 'tooShort' => 'comid最短8位',
                      'max' => 8, 'tooLong' => 'comid最长8位', 'on' => 'add'],
            ['appid', 'default', 'value' => ''],
            ['appid', 'string', 'message' => 'appid必须是字符串',
                      'min' => 18, 'tooShort' => 'appid最短18位',
                      'max' => 18, 'tooLong' => 'appid最长18位'],
            ['appsecret', 'default', 'value' => ''],
            ['appsecret', 'string', 'message' => 'appsecret必须是字符串',
                          'min' => 32, 'tooShort' => 'appsecret最短32位',
                          'max' => 32, 'tooLong' => 'appsecret最长32位'],
            ['state', 'default', 'value' => 0],
            ['state', 'integer', 'message' => 'state必须为数字，或数字字符串'],
            ['state', 'in', 'range' => array(0, 1, 2), 'message' => 'state值必须在0,1,2中选择'],
            ['close_reason', 'default', 'value' => ''],
            ['close_reason', 'string', 'message' => 'close_reason必须是字符串'],
            ['expire_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['expire_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'expire_time非yyyy-MM-dd HH:mm:ss格式'],
            ['add_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['add_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'add_time非yyyy-MM-dd HH:mm:ss格式'],
        ];
    }


    /*添加信息*/
    public function addApp($data)
    {
        // P($data);
        $this->scenario = 'add';
        $data['Opened']['add_time'] = date('Y-m-d H:i:s', time());
        if($this->load($data) and $this->validate()){
            /*计算到期时间*/
            if($this->buyTime > 0){
                $now = date('Y-m-d',time()) . '00:00:00';
                $this->expire_time = date('Y-m-d 00:00:00',strtotime('+' . $this->buyTime . 'year +1day'));
            }
            if($this->save(false)){
                $opened_id = $this->getPrimaryKey();
                return $opened_id;
            }
        }
        return false;
    }

    /*关联查询 APP信息*/
    public function getAppinfo()
    {
        $info = $this->hasOne(AppInfo::className(), ['app_info_id' => 'app_info_id'])
                    ->select(['app_info_id', 'app_name']);
        return $info;
    }



    
}